package service4gateway

import (
	"MyBlogv2/Servers/WebSiteInfo/webSiteInfo"
	"MyBlogv2/Servers/WebSiteInfo/websiteinfoservice"
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/response"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/discov"
	"github.com/zeromicro/go-zero/zrpc"
)

var clientOfWebSiteInfo webSiteInfo.WebSiteInfoServiceClient

func init() {
	conn := zrpc.MustNewClient(zrpc.RpcClientConf{
		Etcd: discov.EtcdConf{
			Hosts: []string{"127.0.0.1:2379"},
			Key:   "websiteinfo.rpc",
		},
	})
	clientOfWebSiteInfo = websiteinfoservice.NewWebSiteInfoService(conn)

}
func GetBanners(c *gin.Context) {
	rsp, err := clientOfWebSiteInfo.GetBanners(c.Request.Context(), &webSiteInfo.WebSiteInfoRequest{})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
	}
	response.ResponseOK(c, rsp.Data)

	return

}
func GetWebsiteInfo(c *gin.Context) {
	rsp, err := clientOfWebSiteInfo.GetWebsiteInfo(c.Request.Context(), &webSiteInfo.WebSiteInfoRequest{})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	var websiteInfoVo models.WebsiteInfoVo
	copier.Copy(&websiteInfoVo, rsp.Data)
	websiteInfoVo.StartTime = helper.TimestampToMyTime(rsp.Data.StartTime)
	websiteInfoVo.LastUpdateTime = helper.TimestampToMyTime(rsp.Data.LastUpdateTime)
	response.ResponseOK(c, websiteInfoVo)
	return

}
func SendCodeOfRegister(c *gin.Context) {
	email := c.Query("email")
	typeOfCode := c.Query("type")
	rsp, err := clientOfWebSiteInfo.SendCodeOfRegister(c.Request.Context(), &webSiteInfo.WebSiteInfoRequest{
		Email:      email,
		TypeOfCode: typeOfCode,
	})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	if rsp.Code == int64(HttpCode.SUCCESS) {
		response.ResponseOK(c, nil)
	} else {
		response.ResponseError(c, HttpCode.AppHttpCodeEnum(rsp.Code))
	}
	return
}
